<template>
  <div class="passwordEdit">
    <!--        <div class="title">修改密码</div>-->
    <div class="form">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="原密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入原密码" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="passwordNew">
          <el-input v-model="form.passwordNew" placeholder="请输入新密码" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" placeholder="请再次输入新密码" type="password"></el-input>
        </el-form-item>
        <el-form-item style="width: 150px">
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { changePassword } from '../../../api/welcome'

export default {
  name: 'passwordEdit',
  data(){
    const equalToPassword = (rule, value, callback) => {
      if (this.form.passwordNew !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return{
      form:{},
      rules: {
        password: [
          { required: true, trigger: "blur", message: "请输入您的原密码" },
        ],
        passwordNew: [
          { required: true, trigger: "blur", message: "请输入您的新密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "blur", message: "请输入验证码" }]
      },
    }
  },
  methods:{
    onSubmit(){
      this.$refs.form.validate(valid => {
        if (valid) {
          changePassword({
            id:this.$store.state.user.userInfo.id,
            password:this.form.password,
            newPassword: this.form.passwordNew}).then(res=>{
            if (res.code === 200){
              this.$message.success('修改成功')
              this.form={}
            }
          })
        }
      });
    }
  }
}
</script>

<style scoped lang="less">
.el-button--primary {
  width: 80px;
  height: 32px;
  border-radius: 34px;
}
.el-button {
  padding: 0;
}
.el-input {
  width: 187px;
  display: block;
}
/deep/.el-input__inner {
  width: 187px;
  border: none;
  border-bottom: 0.5px solid rgba(0, 0, 0, 0.25);;
}
.passwordEdit{
  padding:66px 80px;
  background-color: white;
  border-radius: 20px;
  .title{
    padding-bottom: 20px;
    width: 100px;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: 600;
    color: #333333;
  }
  .form{
    ::v-deep.el-form{
      .el-form-item__content{
        width: 50%;
      }
    }
  }
}

</style>
